package com.example.teaknower.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.example.teaknower.R;
import com.example.teaknower.model.article;
import com.example.teaknower.utils.OkHttpUtil;
import com.example.teaknower.utils.UserUtil;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

public class ArticledetailsActivity extends AppCompatActivity {
    private MainActivity mainActivity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.articledetails_main);

        Intent i=getIntent();
        String id = i.getStringExtra("id");
        article article = getDate(id);
        setImg(article.getImg_url());

        TextView title = findViewById(R.id.article_info_title);
        TextView date = findViewById(R.id.article_info_date);
        TextView content = findViewById(R.id.article_info_content);
        title.setText(article.getTitle());
        date.setText(article.getDate());
        content.setText(article.getContent());
        setToolBar(article.getTitle());

        ImageView iv = findViewById(R.id.article_info_bookmark);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ArticledetailsActivity.this, "点击了收藏", Toast.LENGTH_SHORT).show();
                mainActivity = MainActivity.getMainActivity();
                UserUtil userUtil = new UserUtil(mainActivity);
                String Uid = /*UserUtil.getUserId()*/"8";
                if(Uid.equals("")){
                    Toast.makeText(mainActivity, "请先登录", Toast.LENGTH_SHORT).show();
                    return;
                }
                boolean flag = bookmarkArticle(id,Uid);
                if(flag){
                    Toast.makeText(mainActivity, "收藏成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(mainActivity, "收藏失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private boolean bookmarkArticle(String id,String Uid){
        String s = null;
        try {
            s = OkHttpUtil.getRequest("http://192.168.3.25:8080/Collection/addCollection?id=" + id + "&userId=" + Uid);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private void setImg(String Url){
        new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                try {
                    URL url = new URL(Url);
                    Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
                    return bitmap;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);
                ImageView img = findViewById(R.id.article_info_img);
                img.setImageBitmap(bitmap);
            }
        }.execute();
    }

    private article getDate(String article_id){
        article article = null;
        String s = null;
        try {
            s = OkHttpUtil.getRequest("http://192.168.3.25:8080/Article/findArticleById?id=" + article_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject;
        jsonObject = JSONObject.parseObject(s);
        System.out.println(jsonObject.toJSONString());
        Map<String,Object> mDate = (Map<String,Object>)((Map<String,Object>)jsonObject).get("data");
//        System.out.println(mList.toString());
//        for(int i = 0 ; i < mList.size() ; i++ ){
//            Map<String,Object> m = (Map<String,Object>) mList.get(i);
            String id = mDate.get("id").toString();

            String date = mDate.get("date").toString();
            String img_url = mDate.get("img_url").toString();
            String title = mDate.get("title").toString();
            String content = mDate.get("content").toString();
            article = new article(id, title, img_url, date, content);
//        }
        return article;
    }

    private void setToolBar(String title) {
        Toolbar toolbar =  findViewById(R.id.articledetails_toolbar);
        toolbar.setTitle(title);
        this.setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        this.getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

    //Toolbar的事件---返回
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

}